package com.nstar.base.util
{
	public class DateUtils
	{
		public static const WEEKS_EN:Array = ["Sunday","Monday","Tuesday","Wednesday","ThursDay","Friday","Saturday"];
		public static const WEEKS_CN:Array = ["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
		public static const TERRESTRIAL_BRANCH = ["子时","丑时","寅时","卯时","辰时","巳时","午时","未时","申时","酉时","戌时","亥时"];
		/**
		 * 将形如Sat Dec 30 15:58:41 CST 2006的字符串转为Date型
		 **/
		public static function transDate(str:String):Date {
			trace("input: "+str);
			//str = Sat Dec 30 15:58:41 CST 2006
			/* 0:Sat
			1:Dec
			2:30
			3:15:58:41
			4:CST
			5:2006 */
			//new Date("Mon Aug 8 2008 00:00:00");
			
			if(str == "" || str == null) {
				return null;
			}

			var format:String = "";
			var array:Array = str.split(" ");
			
			add(array[0]);//Sat
			add(array[1]);//Dec
			add(array[2]);//30
			add(array[5]);//2006
			add(array[3]);//15:58:41
			
			trace("format: "+format);
			return new Date(format);
			
			function add(item:String):void {
				format += item + " ";
			}
		}
		
		public static function format(date:Date, pattern:String):String {
			if(date == null)
				return "";
			
			pattern = pattern.replace(/YYYY/g, date.getFullYear());
			pattern = pattern.replace(/MM/g, addZero(date.getMonth()+1));
			pattern = pattern.replace(/DD/g, addZero(date.getDate()));
			
			pattern = pattern.replace(/HH/g, addZero(date.getHours()));
			pattern = pattern.replace(/MS/g, addZero(date.getMinutes()));
			pattern = pattern.replace(/SS/g, addZero(date.getSeconds()));
			return pattern;
		}
		
		private static function addZero(input:Number):String {
			if(input<=9 && input >= 0)
				return "0"+input;
			else
				return input.toString();
		}
		
		public static function formatByString(dateStr:String, pattern:String):String {
			
			var date:Date = transDate(dateStr);
			
			return format(date, pattern);
		}
		
		//取得星期
		public static function week(cn:Boolean = true,date:Date=null):String{
			var date:Date =date?date:(new Date());
			var day:Number = date.getDay();
			return cn?WEEKS_CN[day]:WEEKS_EN[day];
		}
		
		//比较日期，返回天数
		public static function compareDay(firstDate:Date,secondDate:Date):int{
			var first:Number = firstDate.getTime();
			var second:Number = secondDate.getTime();
			return (second-first)/(24*60*60*1000);
		}
		
		public static function compare(firstDate:Date,secondDate:Date):Number{
			var first:Number = firstDate.getTime();
			var second:Number = secondDate.getTime();
			return second - first;
		}
		
		//将秒数转换成 hh:mm:ss
		public static function convert(seconds:Number):String{
			var h:int = seconds/3600;
			var m:int = (seconds-h*3600)/60; //整除
			var s:int = (seconds-h*3600)%60;	//求余	
			return 	addZero(h)+":"+addZero(m)+":"+addZero(s);
		}
		
		//比较时间，返回 hh:mm:ss
		public static function CompareTime(firstDate:Date,secondDate:Date):String{
			var value:Number = secondDate.getTime()- firstDate.getTime();
			return convert(value/1000);
		}
		
		//地支
		public static function getTerrestrialBranch():String{
			var time:String = format(new Date(),"HH");
			switch(time){
				case "23","00":
					return TERRESTRIAL_BRANCH[0];
				case "01","02":
					return TERRESTRIAL_BRANCH[1];
				case "03","04":
					return TERRESTRIAL_BRANCH[2];
				case "05","06":
					return TERRESTRIAL_BRANCH[3];
				case "07","07":
					return TERRESTRIAL_BRANCH[4];
				case "09","10":
					return TERRESTRIAL_BRANCH[5];
				case "11","12":
					return TERRESTRIAL_BRANCH[6];
				case "13","14":
					return TERRESTRIAL_BRANCH[7];
				case "15","16":
					return TERRESTRIAL_BRANCH[8];
				case "17","18":
					return TERRESTRIAL_BRANCH[9];
				case "19","20":
					return TERRESTRIAL_BRANCH[10];
				case "21","22":
					return TERRESTRIAL_BRANCH[11];
				default:
					return "";
			}
		}

		//转换时间，结果为月，周，天，时，分，秒
		private static function convertTime(uptime:Number):String 
		{
			if(uptime <= 0) return "已过期！";
			
			var secs:Number = uptime/1000; 
			var result:String = new String();
			
			var months:int = secs/(30 * 24 * 3600);
			secs = secs % (30 * 24 * 3600);
			if(months > 0) result += months + "个月";
			
			var weeks:int = secs/(7 * 24 * 3600);
			secs = secs % (7 * 24 * 3600);
			if(weeks > 0) result += weeks + "周";
			
			var days:int = secs/(24 * 3600);
			secs = secs % (24 * 3600);
			if(days > 0) result += days + "天";
			
			var hours:int = secs/3600;
			secs = secs % 3600;
			if(hours > 0) result += hours + "小时";
			
			var minutes:int = secs/60;
			secs = secs % 60;
			if(minutes > 0) result += minutes + "分钟";
			
			if(Math.round(secs) > 0) result += Math.round(secs) + "秒";
			
			return result;
		}	
		
		private static function convertTime2Array(uptime:Number):Array 
		{
			if(uptime <= 0) return null;
			
			var secs:Number = uptime/1000; 
			var result:Array = new Array();
			
			var totalDay:Number = Math.round(secs/(24*3600));
			result.push({"total":totalDay});
			
			var months:int = secs/(30 * 24 * 3600);
			secs = secs % (30 * 24 * 3600);
			if(months > 0) {
				result.push({"months":months});
			}else{
				result.push({"months":"00"});
			}
			
			var weeks:int = secs/(7 * 24 * 3600);
			secs = secs % (7 * 24 * 3600);
			if(weeks > 0){
				result.push({"weeks":weeks});
			}else{
				result.push({"weeks":"00"});
			}
			
			var days:int = secs/(24 * 3600);
			secs = secs % (24 * 3600);
			if(days > 0){
				result.push({"days":days});
			}else{
				result.push({"days":"00"});
			}
			
			var hours:int = secs/3600;
			secs = secs % 3600;
			if(hours > 0){
				result.push({"hours":hours});
			}else{
				result.push({"hours":"00"});
			}
			
			var minutes:int = secs/60;
			secs = secs % 60;
			if(minutes > 0){
				result.push({"minutes":minutes});
			}else{
				result.push({"minutes":"00"});
			}
			
			if(Math.round(secs) > 0){
				result.push({"secs":Math.round(secs)});
			}else{
				result.push({"secs":"00"});
			}
			
			return result;
		}		
		
		//取得指定时间到当前时间的倒计时
		public static function 	getCountDown(countDownDate:Date):String{
			return convertTime(countDownDate.getTime() - (new Date()).getTime());
		}
		//返回倒计时的数组表现形式
		public static function getCountDown2Array(countDownDate:Date):Array{
			return convertTime2Array(countDownDate.getTime() - (new Date()).getTime());
		}
		
	}
}